#!/bin/bash

# This set of commands performs motion correction for a set of fucntional runs.
# It is assumed that all fMRI runs come in pais as BOLD and VASO run s
# and that they have a naming  convention as generated fromt he dcm to
# nii conversion in other folders
# in this example pipeline. namely starting with S_sequence number with _bold_ 
# and in file type nii.gz

# This scrips can be execurted in the folder of all the files without an argument
# with ./01_moco_cbvbold.sh

# The output of this script will be a series of files with the prefix moco_*

# This script is based on AFNI (AFNI_25.0.07)

#########################################################
######### generating a mask for motion correction  ######
#########################################################
# The purpose of this is to exclude slab 
# borders that might confuse the motion
# correction algorithm as tissue borders

3dAutomask -overwrite -prefix moma.nii.gz -peels 3 -dilate 2 S*_bold*.nii.gz

3dZeropad                                                                    \
    -A          -4                                                           \
    -P          -4                                                           \
    -I          -4                                                           \
    -S          -4                                                           \
    -R          -4                                                           \
    -L          -4                                                           \
    -overwrite                                                               \
    -prefix     _tmp_AAA.nii.gz                                              \
    moma.nii.gz

3dcalc                                                                       \
    -overwrite                                                               \
    -a          _tmp_AAA.nii.gz                                              \
    -expr       '100*a'                                                      \
    -prefix     _tmp_BBB.nii.gz                                              \
    -datum      short

3dZeropad                                                                    \
    -overwrite                                                               \
    -master     moma.nii.gz                                                  \
    -prefix     _tmp_CCC.nii.gz                                              \
    _tmp_BBB.nii.gz

3dmerge                                                                      \
    -overwrite                                                               \
    -1blur_sigma  3                                                          \
    -prefix       weight_gauss_tmp_.nii.gz _tmp_CCC.nii.gz

3dcalc                                                                       \
    -overwrite                                                               \
    -a          moma.nii.gz                                                  \
    -b          weight_gauss_tmp_.nii.gz                                     \
    -expr       'step(b-55)-0*a'                                             \
    -prefix     moma.nii.gz

\rm _tmp_*

#################################################################
######### generating a mask for motion correction for BOLD ######
#################################################################

cnt=100
echo "starting file loop not-nulled images (clean BOLD images)"

for filename in ./S*_bold*.nii.gz
do
echo $filename


# I am generating a reference image from the beginnin of the 
# first run that all later tim points and runsa are aligned to
# the first few volumes are non-steady state and are not used.
3dTstat -mean -prefix nn_reference.nii.gz $filename'[3..5]'


## magaginf time points at beginnign and end of each run
# The fist time point is overwritten because it is non-steady state
# Also, the last two wolumes are discarded because they are noise reference volumes. 
# they might be usefull for NORDIC, which we are not using here
NumVol=`3dinfo -nv $filename`
3dTcat -prefix moco_bold_${cnt}.nii.gz $filename'[1]' $filename'[1..'`expr $NumVol - 3`']' -overwrite



# performing motion correction
# the shift_rotate option allows only linear deformations, when removed, sheering is also allowed
3dAllineate                                                                  \
    -1Dmatrix_save  ALLIN_bold_${cnt}.aff12.1D                               \
    -cost           lpa                                                      \
    -prefix         moco_bold_${cnt}.nii.gz                                  \
    -base           nn_reference.nii.gz                                      \
    -source         moco_bold_${cnt}.nii.gz                                  \
    -weight         moma.nii.gz                                              \
    -warp           shift_rotate                                             \
    -final          wsinc5                                                   \
    -overwrite

cnt=$(($cnt+1))

done


##################################
######### the same for VASO ######
##################################

cnt=100
echo "starting file loop blood-nulled images (VASO with BOLD contamination)"

for filename in ./S*_cbv*.nii.gz
do
echo $filename
3dTstat -mean -prefix n_reference.nii.gz $filename'[3..5]'

NumVol=`3dinfo -nv $filename`
3dTcat -prefix moco_cbv_${cnt}.nii.gz $filename'[1]' $filename'[1..'`expr $NumVol - 3`']' -overwrite

3dAllineate                                                                   \
    -1Dmatrix_save  ALLIN_cbv_${cnt}.aff12.1D                                 \
    -cost           lpa                                                       \
    -prefix         moco_cbv_${cnt}.nii.gz                                    \
    -base           n_reference.nii.gz                                        \
    -source         moco_cbv_${cnt}.nii.gz                                    \
    -weight         moma.nii.gz                                               \
    -warp           shift_rotate                                              \
    -final          wsinc5                                                    \
    -overwrite

cnt=$(($cnt+1))

done 

